import React from "react";
import { Button, Checkbox, Form, Input, message } from "antd";
import "./index.css";
import http from "../../utils";
import { useNavigate } from "react-router-dom";
const Index = () => {
  const navigate = useNavigate();
  const onFinish = (values) => {
    // console.log(values);
    http.post("/login", values).then((res) => {
      // console.log(res);
      const { code, msg, token ,user} = res.data;
      if (code === 200) {
        localStorage.setItem("token", token);
        localStorage.setItem("user", JSON.stringify(user));
        message.success(msg);
        setTimeout(() => {
          navigate("/");
        }, 1500);
      } else if (code === 200) {
        message.error(msg);
      } else {
        message.error(msg);
      }
    });
  };
  return (
    <Form
    className="form"
      style={{ minWidth: "400px" }}
      labelCol={{ span: 5 }}
      wrapperCol={{ span: 16 }}
      onFinish={onFinish}
    >
      <h1>用户登录</h1>
      <Form.Item label="用户名" name="username">
        <Input />
      </Form.Item>
      <Form.Item label="密码" name="password">
        <Input.Password />
      </Form.Item>
      <Form.Item wrapperCol={{ offset: 5 }}>
        <Checkbox>我已阅读并同意xxxxxxx</Checkbox>
      </Form.Item>
      <Form.Item wrapperCol={{ offset: 5 }}>
        <Button type="primary" htmlType="submit">
          登录
        </Button>
      </Form.Item>
    </Form>
  );
};

export default Index;
